package com.pmcc.platform.giftGoods.service;


import com.pmcc.miniProgram.service.IndexService;
import com.pmcc.platform.giftGoods.entity.Goods;
import com.pmcc.platform.giftGoods.entity.GoodsDetailImage;
import com.pmcc.platform.giftGoods.entity.GoodsImage;
import com.pmcc.platform.giftGoods.repository.GoodsDetailImageRepository;
import com.pmcc.platform.giftGoods.repository.GoodsImageRepository;
import com.pmcc.platform.giftGoods.repository.GoodsRepository;
import com.pmcc.platform.giftGoods.vo.GoodsVo;
import com.pmcc.platform.shopping.entity.GoodsGift;
import com.pmcc.platform.shopping.repository.GoodsGiftRepository;
import com.pmcc.utils.*;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: vidalGift
 * @description:
 * @author: Du
 * @create: 2020-12-11 15:29
 **/
@Service
@Transactional
public class GoodsService {

    @Autowired
    GoodsRepository goodsRepository;

    @Autowired
    GoodsImageRepository goodsImageRepository;

    @Autowired
    GoodsImageService goodsImageService;

    @Autowired
    GoodsDetailImageService goodsDetailImageService;

    @Autowired
    GoodsDetailImageRepository goodsDetailImageRepository;

    @Autowired
    GoodsGiftRepository goodsGiftRepository;

    @Autowired
    IndexService indexService;

    public ApiResultBean queryAllGoods(AntPage antPage,String name,String remark) {
        ApiResultBean apiResultBean = new ApiResultBean();
        Page<Goods>  goods=goodsRepository.findAllByDelFlagAndNameContainsAndRemarkContainsOrderByCreateTimeDesc("0",name,remark,antPage.getPageRequest());

        if (goods.getSize() == 0) {
            apiResultBean.setCode(ApiResultBean.FAIL);
            apiResultBean.setMsg("查询为空");
            return apiResultBean;
        }
        List<GoodsVo> goodsVos = new ArrayList<>();
        for (Goods goods1 : goods) {
            GoodsVo goodsVo = new GoodsVo();
            goodsVo.setGoodsId(goods1.getId());
            goodsVo.setThreshold(goods1.getThreshold());
            goodsVo.setSysCode(goods1.getSysCode());
            goodsVo.setSpecification(goods1.getSpecification());
            goodsVo.setName(goods1.getName());
        //    goodsVo.setSortNo(goods1.getSortNo());
            goodsVo.setPrice(goods1.getPrice());
            goodsVo.setImages(goods1.getImages());
            goodsVo.setRemark(goods1.getRemark());
            List<GoodsGift> goodsGift=goodsGiftRepository.findAllByGoodsIdAndDelFlag(goods1.getId(),"0");
            if(goodsGift.size()>0){
                int getNum=goodsGiftRepository.countGoodNum(goods1.getId(),"1","0");
                goodsVo.setGetNum(getNum);
            }

            List<GoodsImage> goodsImageList = goodsImageRepository.findAllByGoodsIdAndDelFlag(goods1.getId(), "0");
            List<String> list=new JSONArray();
            for (GoodsImage goodsImage: goodsImageList ) {
                list.add(goodsImage.getImagePath());
            }
            goodsVo.setImageList(list);
            List<GoodsDetailImage> goodsDetailImages = goodsDetailImageRepository.findAllByGoodsIdAndDelFlag(goods1.getId(), "0");
            List<String> list1=new ArrayList<>();
            for (GoodsDetailImage goodsDetailImage: goodsDetailImages) {
                list1.add(goodsDetailImage.getImagePath());
            }
            goodsVo.setDetailImageList(list1);
            goodsVos.add(goodsVo);
        }



        apiResultBean.setData(goodsVos,antPage,goods.getTotalElements());
        apiResultBean.setCode(ApiResultBean.SUCCESS);
        apiResultBean.setMsg("查询成功");
        return apiResultBean;

    }


    public ApiResultBean addGoods(GoodsVo goodsVo) {
        ApiResultBean apiResultBean = new ApiResultBean();
        if (goodsVo == null) {
            apiResultBean.setCode(ApiResultBean.FAIL);
            apiResultBean.setMsg("不能添加空");
            return apiResultBean;
        }
        Goods goods = new Goods();

     //   giftGoods.setSysCode(goodsVo.getSysCode());
        goods.setSpecification(goodsVo.getSpecification());
        goods.setThreshold(goodsVo.getThreshold());
     //   giftGoods.setSortNo(goodsVo.getSortNo());
        goods.setPrice(goodsVo.getPrice());
        goods.setName(goodsVo.getName());

        goods.setImages(goodsVo.getImages());
        goods.setRemark(goodsVo.getRemark());

        goodsRepository.saveAndFlush(goods);
        List<String> goodsImageList = goodsVo.getImageList();
        if ( goodsImageList.size() > 0) {
            for (String goodsImage : goodsImageList) {
                GoodsImage goodsImage1=new GoodsImage();
                goodsImage1.setGoodsId(goods.getId());
                goodsImage1.setImagePath(goodsImage);
                goodsImageRepository.save(goodsImage1);
            }
        }
        List<String> goodsDetailImages = goodsVo.getDetailImageList();
        if ( goodsDetailImages.size() > 0) {
            for (String goodsDetailImage : goodsDetailImages) {
                GoodsDetailImage goodsDetailImage1=new GoodsDetailImage();
                goodsDetailImage1.setGoodsId(goods.getId());
                goodsDetailImage1.setImagePath(goodsDetailImage);
                goodsDetailImageRepository.save(goodsDetailImage1);
            }
        }

        apiResultBean.setMsg("添加成功");
        apiResultBean.setCode(ApiResultBean.SUCCESS);
        return apiResultBean;
    }


    public ApiResultBean deleteGoods(String goodsId) {
        ApiResultBean apiResultBean = new ApiResultBean();
        if (StringUtil.isEmpty(goodsId)) {
            apiResultBean.setCode(ApiResultBean.FAIL);
            apiResultBean.setMsg("参入的id为空");
            return apiResultBean;
        }
        Goods goods = goodsRepository.findOne(goodsId);
        goods.setDelFlag("1");
        goodsRepository.save(goods);
        apiResultBean.setMsg("删除成功");
        apiResultBean.setCode(ApiResultBean.SUCCESS);
        return apiResultBean;
    }

    public ApiResultBean modifyGoods(GoodsVo goodsVo) {
        ApiResultBean apiResultBean = new ApiResultBean();
        if (goodsVo == null) {
            apiResultBean.setCode(ApiResultBean.FAIL);
            apiResultBean.setMsg("传入的对象为空");
            return apiResultBean;
        }
        Goods goods = goodsRepository.findOne(goodsVo.getGoodsId());



            goods.setSysCode(goodsVo.getSysCode());


            goods.setSpecification(goodsVo.getSpecification());


            goods.setThreshold(goodsVo.getThreshold());


         //   giftGoods.setSortNo(goodsVo.getSortNo());


            goods.setPrice(goodsVo.getPrice());


            goods.setName(goodsVo.getName());


            goods.setImages(goodsVo.getImages());

            goods.setRemark(goodsVo.getRemark());

            List<GoodsImage> goodsImageList1=goodsImageRepository.findAllByGoodsIdAndDelFlag(goodsVo.getGoodsId(),"0");
            if(goodsImageList1.size()>0){
                for (GoodsImage goodsImage: goodsImageList1 ) {
                    goodsImageRepository.delete(goodsImage.getId());
                }
            }

            List<String> goodsImageList = goodsVo.getImageList();
            if ( goodsImageList.size() > 0) {
                for (String goodsImage : goodsImageList) {
                    GoodsImage goodsImage1=new GoodsImage();
                    goodsImage1.setGoodsId(goodsVo.getGoodsId());
                    goodsImage1.setImagePath(goodsImage);
                    goodsImageRepository.save(goodsImage1);
                }
            }


            List<GoodsDetailImage> goodsDetailImages1 =goodsDetailImageRepository.findAllByGoodsIdAndDelFlag(goodsVo.getGoodsId(),"0");
            if(goodsDetailImages1.size()>0){
                for (GoodsDetailImage goodsDetailImage: goodsDetailImages1 ) {
                    goodsDetailImageRepository.delete(goodsDetailImage.getId());

                }
            }
            List<String> goodsDetailImages = goodsVo.getDetailImageList();
            if (goodsDetailImages.size() > 0) {
                for (String goodsDetailImage : goodsDetailImages) {
                    GoodsDetailImage goodsDetailImage1=new GoodsDetailImage();
                    goodsDetailImage1.setGoodsId(goodsVo.getGoodsId());
                    goodsDetailImage1.setImagePath(goodsDetailImage);
                    goodsDetailImageRepository.save(goodsDetailImage1);
                }
            }



        apiResultBean.setCode(ApiResultBean.SUCCESS);
        apiResultBean.setMsg("修改成功");
        return apiResultBean;
    }

    public  ApiResultBean selectGoodsGift(AntPage antPage,String goodsName){
        ApiResultBean apiResultBean=new ApiResultBean();
        Page<Goods> goods=goodsRepository.findAllByNameContainsAndDelFlag(goodsName,"0",antPage.getPageRequest());
        apiResultBean.setData(goods,antPage);
        return  apiResultBean;
    }

}
